﻿using System;
using System.Collections.Generic;
using System.Linq;
using DevComponents.DotNetBar;
using FastReport;
using FastReport.Data;
using FastReport.Preview;
using Kugar.Core.BaseStruct;
using Kugar.Core.Printer;
using Kugar.Core.ExtMethod;

namespace Kugar.UI.WinForm.Print
{
    static class Program
    {
        //private const float DefaultElementWidth = 2.25f;
        //private const float DefaultElementHeight = 0.5f;
        //private const float DefaultElementPadding = 10f;

        private static MainOrder t = null;
        private static Report report = null;
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            //Application.EnableVisualStyles();
            //Application.SetCompatibleTextRenderingDefault(false);
            //Application.Run(new Form1());

            //return;

            var t1=new object();



            t = new MainOrder();
            t.UnitID = "sssss";
            t.Product = new ProductInfo();
            t.Product.MainID = "MainID-001";
            t.Product.ProductID = "PID-001";




            for (int i = 0; i < 10; i++)
            {
                var detailItem = new VDetailOrder();

                detailItem.MainID = "DetailMainID-00" + i;
                detailItem.Price = 10;
                detailItem.Qty = 20;
                detailItem.Color.GroupName = "Color";
                detailItem.Color.HeaderText = "颜色";


                detailItem.Color["Red"] = RandomEx.Next(30);
                detailItem.Color["Black"] = RandomEx.Next(30);

                detailItem.Size.GroupName = "Size";
                detailItem.Size.HeaderText = "尺码";
                detailItem.Size["L2"] = RandomEx.Next(30);                
                detailItem.Size["X"] = RandomEx.Next(30);
                detailItem.Size["L1"] = RandomEx.Next(30);

                //detailItem.Size["L3"] = RandomEx.Next(30);
                //detailItem.Size["L4"] = RandomEx.Next(30);
                //detailItem.Size["L5"] = RandomEx.Next(30);
                //detailItem.Size["L6"] = RandomEx.Next(30);
                //detailItem.Size["L7"] = RandomEx.Next(30);


                detailItem.Product = new ProductInfo();
                detailItem.Product.MainID = "PMainID-00" + i;
                detailItem.Product.ProductID = "PID-00" + i;



                t.Detail.Add(detailItem);
            }


            var fp = new FasterReportProvider(new PrintFormatManageDataProvider());

            var moduleProvider = fp.GetModuleProvider("ddddd",t);

            var frm = new PrintManager(moduleProvider);

            frm.ShowDialog();

            

            //var format=moduleProvider.CreateNew();

            //moduleProvider.Add(format.Name,format.PrintFormat);




            return;

            #region "old code"

            //var schema = UniversalPrintSchema.LoadFrom(t.GetType());

            //report = new Report();

            ////report.StartReport += report_StartReport;

            //ReportPage page = new ReportPage();
            //page.CreateUniqueName();

            //page.TopMargin = 10.0f;
            //page.LeftMargin = 10.0f;
            //page.RightMargin = 10.0f;
            //page.BottomMargin = 10.0f;
            //report.Pages.Add(page);


            //if (schema.HasReportHead)
            //{
            //    page.ReportTitle = new ReportTitleBand();
            //    page.ReportTitle.CreateUniqueName();
            //    page.ReportTitle.Height = 1.5f * Units.Centimeters;

            //    FillElement("报表头", page.PaperWidth, page.ReportTitle, schema.ReportHead.Fields);

            //    report.RegisterData(ObjToDataTable(schema.ReportHead, t, "报表头"), "报表头");
            //    report.GetDataSource("报表头").Enabled = true;
            //}

            //if (schema.HasPageHead)
            //{
            //    page.PageHeader = new PageHeaderBand();
            //    page.PageHeader.CreateUniqueName();
            //    page.PageHeader.Height = 2.0f * Units.Centimeters;

            //    FillElement("页眉", page.PaperWidth, page.PageHeader, schema.PageHead.Fields);

            //    report.RegisterData(ObjToDataTable(schema.PageHead, t, "页眉"), "页眉");
            //    report.GetDataSource("页眉").Enabled = true;
            //}

            //if (schema.HasDetails)
            //{
            //    //page.PageHeader.Height += (1*Units.Centimeters);

            //    for (int i = 0; i < schema.Details.Count; i++)
            //    {
            //        var detailName = string.IsNullOrWhiteSpace(schema.Details[i].DetailName)
            //                             ? "明细" + i
            //                             : schema.Details[i].DetailName;

            //        report.RegisterData(ObjToDataTable(schema.Details[i], t.Detail, detailName), detailName);
            //        report.GetDataSource(detailName).Enabled = true;

            //        //创建数据区
            //        var detailBand = new DataBand();
            //        detailBand.CreateUniqueName();

            //        detailBand.BeforePrint += detailBand_BeforePrint;

            //        //创建数据区页眉
            //        detailBand.Header = new DataHeaderBand();
            //        detailBand.Header.CreateUniqueName();
            //        detailBand.Header.FillColor = Color.DarkGray;

            //        detailBand.Name = schema.Details[i].BindingProperty;
            //        detailBand.DataSource = report.GetDataSource(detailName);

            //        var startPos = new PointF(1, 0);

            //        var hasMutilColumn = schema.Details[i].DynamicColumnConfig.Any();

            //        var titleHeight = hasMutilColumn
            //                              ? 2 * DefaultElementHeight * Units.Centimeters
            //                              : DefaultElementHeight * Units.Centimeters;

            //        var dataCellHeight = DefaultElementHeight * Units.Centimeters;

            //        //添加列
            //        for (int j = 0; j < schema.Details[i].Fields.Count; j++)
            //        {
            //            var printElement = schema.Details[i].Fields[j];

            //            ReportComponentBase cell = null;

            //            //var cell = CreateLabel(string.Format("[明细{0}.{1}]",i,printElement.BindingColumn), startPos);
            //            //cell.Height = dataCellHeight;


            //            ReportComponentBase title = null;

            //            if (printElement.IsDynamicColumn)//.DataType.IsAssignableFrom(typeof(DynamicColumn)))
            //            {
            //                var tableColmn = new TableObject();

            //                tableColmn.Height = titleHeight;
            //                tableColmn.Width = (1.5f * DefaultElementWidth * Units.Centimeters);

            //                //预设为2行,第二行表头默认为4格
            //                tableColmn.RowCount = 2;
            //                tableColmn.ColumnCount = 4;

            //                //设置双层表头第一层中文本的对齐以及单元格合并
            //                tableColmn.Rows[0][0].ColSpan = 4;
            //                tableColmn.Rows[0][0].Text = printElement.HeaderText;
            //                tableColmn.Rows[0][0].HideValue = printElement.Name;// printElement.BindingColumn;
            //                tableColmn.Rows[0][0].HorzAlign = HorzAlign.Center;
            //                tableColmn.Rows[0][0].VertAlign = VertAlign.Center;
            //                //表头位置以及框线
            //                tableColmn.Border.Lines = BorderLines.All;
            //                tableColmn.Left = startPos.X;
            //                tableColmn.Top = startPos.Y;

            //                //初始化明细列头的宽度
            //                foreach (TableColumn column in tableColmn.Columns)
            //                {
            //                    column.Width = tableColmn.Width / tableColmn.ColumnCount;
            //                }


            //                var cellTable = new TableObject();
            //                cellTable.RowCount = 1;
            //                cellTable.ColumnCount = 4;
            //                cellTable.Border.Lines = BorderLines.All;
            //                cellTable.Width = tableColmn.Width;

            //                for (int k = 0; k < cellTable.ColumnCount; k++)
            //                {
            //                    cellTable.Columns[k].Width = cellTable.Width / cellTable.ColumnCount;
            //                    cellTable.Rows[0][k].Border.Lines = BorderLines.All;
            //                }

            //                cellTable.Rows[0][0].ColSpan = 4;
            //                cellTable.Rows[0][0].HideValue = printElement.Name;
            //                cellTable.Rows[0][0].Text = "运行时动态列显示";
            //                //cellTable.Rows[0][1].HideValue = printElement.BindingColumn;

            //                // tableColmn.BeforePrint += headerTextTable_ManualBuild;
            //                //cellTable. += cellTable_BeforePrint;



            //                title = tableColmn;
            //                cell = cellTable;
            //            }
            //            else
            //            {

            //                var textTitle = CreateLabel(printElement.HeaderText, startPos);
            //                textTitle.Height = titleHeight;// DefaultElementHeight*Units.Centimeters;
            //                textTitle.HorzAlign = HorzAlign.Center;
            //                textTitle.Border.Lines = BorderLines.All;

            //                title = textTitle;

            //                cell = CreateLabel(string.Format("[{0}.{1}]", detailName, printElement.BindingColumn), startPos);
            //                cell.Height = dataCellHeight;
            //                //cell.Left = startPos.X;
            //                //cell.Top = startPos.Y;

            //                //cell.BeforePrint+=cell_BeforePrint;
            //            }

            //            detailBand.Header.Objects.Add(title);
            //            detailBand.Objects.Add(cell);
            //            cell.Left = startPos.X;
            //            cell.Top = startPos.Y;

            //            startPos.X = title.Left + title.Width;// +title.Border.Width;

            //            if (j == 0)
            //            {
            //                title.Border.Lines = BorderLines.Left;
            //                cell.Border.Lines = BorderLines.Left;
            //            }


            //            cell.Border.Lines = cell.Border.Lines | BorderLines.Right;

            //        }

            //        //detailBand.BeforeLayout += detailBand_BeforePrint;
            //        //detailBand.AfterData += detailBand_BeforePrint;
            //        //detailBand.AfterPrint += detailBand_BeforePrint;
            //        //detailBand.BeforePrint += detailBand_BeforePrint;

            //        //创建数据区页脚
            //        detailBand.Footer = new DataFooterBand();
            //        detailBand.Footer.CreateUniqueName();
            //        detailBand.Footer.Height = 0.6f * Units.Centimeters;

            //        detailBand.Height = dataCellHeight;// detailBand.CalcHeight();
            //        detailBand.Header.Height = titleHeight;// detailBand.Header.CalcHeight();

            //        page.Bands.Add(detailBand);

            //    }
            //}

            //if (schema.HasPageFooter)
            //{
            //    page.PageFooter = new PageFooterBand();
            //    page.PageFooter.CreateUniqueName();
            //    page.PageFooter.Height = 2.0f * Units.Centimeters;

            //    FillElement("页脚", page.PaperWidth, page.PageFooter, schema.PageFooter.Fields);

            //    report.RegisterData(ObjToDataTable(schema.PageFooter, t, "页脚"), "页脚");
            //    report.GetDataSource("页脚").Enabled = true;
            //}

            //if (schema.HasReportFooter)
            //{
            //    page.ReportSummary = new ReportSummaryBand();
            //    page.ReportSummary.CreateUniqueName();
            //    page.ReportSummary.Height = 2.0f * Units.Centimeters;

            //    FillElement("报表尾", page.PaperWidth, page.ReportSummary, schema.ReportFooter.Fields);
            //    report.RegisterData(ObjToDataTable(schema.ReportFooter, t, "报表尾"), "报表尾");
            //    report.GetDataSource("报表尾").Enabled = true;
            //}


            //FastReport.Utils.Config.PreviewSettings.ShowInTaskbar = true;
            //FastReport.Utils.Config.PreviewSettings.PagesInCache = 1;

            //report.StartReport += report_StartReport;

            ////FastReport.Utils.Config.DesignerSettings.DesignerLoaded += DesignerSettings_DesignerLoaded;
            ////FastReport.Utils.Config.DesignerSettings.CustomPreviewReport += DesignerSettings_CustomPreviewReport;

            ////report.FinishReport += report_FinishReport;

            ////report.StartReport+=report_StartReport;
            //report.Design(true);

            //var pp = report.Designer.Modified;

            ////Application.EnableVisualStyles();
            ////Application.SetCompatibleTextRenderingDefault(false);
            ////Application.Run(new Form1());

            //var stream = new MemoryStream();

            //report.Save(stream);

            //stream.Position = 0;

            //var newreport = new Report();

            //newreport.Load(stream);
            //newreport.GetDataSource("页眉").Clear();
            //newreport.RegisterData(ObjToDataTable(schema.PageHead, t, "页眉"), "页眉");
            //newreport.GetDataSource("页眉").Enabled = true;

            //newreport.GetDataSource("明细0").Clear();
            //newreport.RegisterData(ObjToDataTable(schema.Details[0], t.Detail, "明细0"), "明细0");
            //newreport.GetDataSource("明细0").Enabled = true;

            //newreport.StartReport += report_StartReport;

            //newreport.Design(true);

            //stream.Dispose();

            #endregion
        }

        #region 注释代码
        
        //static void detailBand_BeforePrint(object sender, EventArgs e)
        //{
        //    // report.PreparedPages.Clear();
        //    //throw new NotImplementedException();
        //}

        //static void dynamicColumnData_BeforePrint(object sender, EventArgs e)
        //{
        //    var table = (TableObject)sender;

        //    var dataBand = (DataBand)table.Parent;

        //    var row = (DataRow)dataBand.DataSource.CurrentRow;


        //    var dynamicColumn = (DynamicColumn)row[table.Rows[0][0].HideValue];

        //    var columnConfig = dynamicColumn.Collection.DynamicColumns[dynamicColumn.GroupName];

        //    table.ColumnCount = columnConfig.HeaderTextList.Count;
        //    table.RowCount = 1;
        //    table.Rows[0].Width = table.Width;
        //    table.Rows[0][0].ColSpan = 1;

        //    int i = 0;

        //    foreach (var headerKey in columnConfig.HeaderTextList)
        //    {
        //        if (dynamicColumn.ContainsKey(headerKey.Key))
        //        {
        //            table.Rows[0][i].Text = dynamicColumn[headerKey.Key].ToStringEx();
        //        }
        //        else
        //        {

        //        }
        //        table.Rows[0][i].HorzAlign = HorzAlign.Center;
        //        table.Rows[0][i].VertAlign = VertAlign.Center;
        //        table.Rows[0][i].Border.Lines = BorderLines.All;
        //        i += 1;
        //    }

        //    AutoSetColumnWidth(table);
        //    AutoSetTableRowBorder(table, 0);

        //    //table.PrintRow(0);
        //    //table.PrintColumns();
        //}

        //static void AutoSetColumnWidth(TableObject table)
        //{
        //    foreach (TableColumn column in table.Columns)
        //    {
        //        column.Width = table.Width / table.ColumnCount;
        //    }
        //}

        //static void AutoSetTableRowBorder(TableObject table, int rowIndex)
        //{
        //    if (table.ColumnCount < 0)
        //    {
        //        return;
        //    }

        //    for (int i = 0; i < table.ColumnCount; i++)
        //    {
        //        table[i, rowIndex].Border.Lines = BorderLines.All;
        //    }


        //}

        //static void dynamicColumnHeader_BeforePrint(object sender, EventArgs e)
        //{
        //    var table = (TableObject)sender;

        //    var dataBand = (DataBand)table.Parent.Parent;

        //    var detailData = t.FastGetValue(dataBand.Name);

        //    var headerConfig = detailData.FastGetValue<Dictionary<string, ColumnConfig>>("DynamicColumns");

        //    var lst = headerConfig[table.Rows[0][0].HideValue];



        //    //TODO:检查对象是否为空

        //    table.ColumnCount = lst.HeaderTextList.Count;
        //    table.RowCount = 2;
        //    table.Rows[0][0].ColSpan = table.ColumnCount;
        //    table.Rows[0][0].HorzAlign = HorzAlign.Center;
        //    table.Rows[0][0].VertAlign = VertAlign.Center;
        //    table.Rows[0][0].Text = lst.HeaderText;

        //    int i = 0;
        //    foreach (var headerText in lst.HeaderTextList)
        //    {
        //        table.Rows[1][i].Text = headerText.Key;
        //        table.Rows[1][i].HorzAlign = HorzAlign.Center;
        //        table.Rows[1][i].VertAlign = VertAlign.Center;
        //        i += 1;
        //    }

        //    AutoSetColumnWidth(table);
        //    AutoSetTableRowBorder(table, 1);

        //}

        //static void report_StartReport(object sender, EventArgs e)
        //{
        //    //report.PreparedPages.CanUploadToCache = false;
        //    //report.PreparedPages.Clear();



        //    //report.Page.Refresh();
        //    //report.Page.Clear();

        //    //foreach (Base base2 in report.Dictionary.AllObjects)
        //    //{
        //    //    if (base2 is DataComponentBase)
        //    //    {
        //    //        (base2 as DataComponentBase).InitializeComponent();
        //    //    }
        //    //}

        //    var tempReport = (Report)sender;


        //    foreach (var band in ((ReportPage)tempReport.Pages[0]).Bands)
        //    {
        //        if (band is DataBand)
        //        {
        //            var dataBand = (DataBand)band;

        //            foreach (ReportComponentBase ctrl in dataBand.Header.Objects)
        //            {
        //                if (ctrl is TableObject)
        //                {
        //                    //((TableObject)ctrl).OnBeforePrint(EventArgs.Empty);

        //                    dynamicColumnHeader_BeforePrint(ctrl, EventArgs.Empty);
        //                }
        //                //ctrl.OnBeforePrint(EventArgs.Empty);
        //            }

        //            foreach (ReportComponentBase ctrl in dataBand.Objects)
        //            {

        //                if (ctrl is TableObject)
        //                {
        //                    dynamicColumnData_BeforePrint(ctrl, EventArgs.Empty);
        //                }
        //            }


        //            //dataBand.GetData();

        //            //((DataBand)band).OnAfterData(EventArgs.Empty);
        //        }
        //    }

        //    //var report = (Report) sender;

        //    //var page = (ReportPage)report.Pages[0];

        //    //foreach (var band in page.Bands)
        //    //{
        //    //    if (!(band is DataBand))
        //    //    {
        //    //        continue;
        //    //    }

        //    //    var databand = (DataBand) band;

        //    //    foreach (var titleObj in databand.Header.Objects)
        //    //    {
        //    //        if (!(titleObj is TextObject))
        //    //        {
        //    //            continue;
        //    //        }

        //    //        var textTitle = (TextObject) titleObj;

        //    //        if (!string.IsNullOrWhiteSpace(textTitle.HideValue))
        //    //        {
        //    //            textTitle.Text = "dddd";

        //    //            float x =textTitle.Left;

        //    //            for (int i = 0; i < 4; i++)
        //    //            {
        //    //                var header = CreateLabel("S" +i,new PointF(x,1.75f*Units.Centimeters));

        //    //                x += 1*Units.Centimeters;

        //    //                databand.Objects.Add(header);

        //    //                //textTitle.AllObjects.Add(header);                            
        //    //            }
        //    //        }
        //    //    }


        //    //}

        //    //throw new NotImplementedException();
        //}

        //private static DataTable ObjToDataTable(UniversalBlock scheme, object obj, string tableName)
        //{
        //    var dt = new DataTable(tableName);
        //    DataRow row = dt.NewRow();
        //    foreach (var field in scheme.Fields)
        //    {
        //        DataColumn column = null;

        //        if (string.IsNullOrWhiteSpace(field.BindingColumn))
        //        {
        //            column = new DataColumn(field.Name, field.DataType);
        //            column.DefaultValue = field.Text;
        //            dt.Columns.Add(column);
        //            row[field.Name] = field.Text;
        //        }
        //        else
        //        {
        //            column = new DataColumn(field.BindingColumn, field.DataType);
        //            column.DefaultValue = field.Text;
        //            dt.Columns.Add(column);

        //            var value = obj.FastGetValue(field.BindingColumn);

        //            if (value == null)
        //            {
        //                value = DBNull.Value;
        //            }

        //            row[column] = value;// obj.FastGetValue(field.BindingColumn);
        //        }
        //    }

        //    dt.Rows.Add(row);



        //    return dt;

        //}

        //private static DataTable ObjToDataTable(UniversalBlock scheme, IEnumerable obj, string tableName)
        //{
        //    var dt = new DataTable(tableName);


        //    foreach (var field in scheme.Fields)
        //    {
        //        DataColumn column = null;

        //        Type dataType = null;

        //        if (field.IsDynamicColumn)
        //        {
        //            dataType = typeof(DynamicColumn);
        //        }
        //        else
        //        {
        //            dataType = field.DataType;
        //        }

        //        column = new DataColumn(field.BindingColumn, dataType);
        //        column.DefaultValue = field.Text;
        //        dt.Columns.Add(column);
        //    }

        //    foreach (var o in obj)
        //    {
        //        DataRow row = dt.NewRow();

        //        foreach (var field in scheme.Fields)
        //        {
        //            if (string.IsNullOrWhiteSpace(field.BindingColumn))
        //            {
        //                row[field.Name] = field.Text;
        //            }
        //            else
        //            {
        //                var value = o.FastGetValue(field.BindingColumn);

        //                if (value == null)
        //                {
        //                    value = DBNull.Value;
        //                }

        //                row[field.BindingColumn] = value;
        //            }
        //        }

        //        dt.Rows.Add(row);
        //    }

        //    //foreach (var field in scheme.Fields)
        //    //{
        //    //    DataColumn column = null;

        //    //    if (string.IsNullOrWhiteSpace(field.BindingColumn))
        //    //    {
        //    //        column = new DataColumn(field.Name, field.DataType);
        //    //        column.DefaultValue = field.Text;
        //    //        dt.Columns.Add(column);
        //    //        row[field.Name] = field.Text;
        //    //    }
        //    //    else
        //    //    {
        //    //        column = new DataColumn(field.BindingColumn, field.DataType);
        //    //        column.DefaultValue = field.Text;
        //    //        dt.Columns.Add(column);

        //    //        var value = obj.FastGetValue(field.BindingColumn);

        //    //        if (value == null)
        //    //        {
        //    //            value = DBNull.Value;
        //    //        }

        //    //        row[column] = value;// obj.FastGetValue(field.BindingColumn);
        //    //    }
        //    //}

        //    //dt.Rows.Add(row);

        //    return dt;
        //}

        //private static void FillElement(string datasourceName, float totalWidth, BandBase band, List<PrintElementItem> elementItems)
        //{
        //    var startPos = new PointF();
        //    startPos.X = 0.1f * Units.Centimeters;
        //    startPos.Y = 0.05f * Units.Centimeters;

        //    foreach (var item in elementItems)
        //    {
        //        if (string.IsNullOrWhiteSpace(item.BindingColumn))
        //        {
        //            var newElements = CreateLabel(item.Text, startPos);

        //            band.Objects.Add(newElements);

        //            startPos.X = newElements.Left + newElements.Width;
        //        }
        //        else
        //        {
        //            var newElements = CreateBindingItem(item.HeaderText, datasourceName + "." + item.BindingColumn, startPos);
        //            band.Objects.Add(newElements[0]);
        //            band.Objects.Add(newElements[1]);
        //            startPos.X = newElements[1].Left + newElements[1].Width;
        //        }

        //        startPos.X += DefaultElementPadding;

        //        if (startPos.X + (DefaultElementWidth * Units.Centimeters) * 2 > totalWidth * Units.Millimeters)
        //        {
        //            startPos.X = 0.1f * Units.Centimeters;

        //            startPos.Y = startPos.Y + (DefaultElementHeight + 0.05f) * Units.Centimeters;
        //        }
        //    }

        //    band.Height = startPos.Y + (DefaultElementHeight + 0.6f) * Units.Centimeters;
        //}

        //private static TextObject CreateLabel(string text, PointF startPos)
        //{
        //    TextObject titleText = new TextObject();
        //    titleText.CreateUniqueName();
        //    titleText.Text = text;

        //    titleText.Left = startPos.X;
        //    titleText.Top = startPos.Y;
        //    titleText.Width = DefaultElementWidth * Units.Centimeters;
        //    titleText.Height = DefaultElementHeight * Units.Centimeters;
        //    titleText.HorzAlign = HorzAlign.Left;
        //    titleText.VertAlign = VertAlign.Center;
        //    titleText.Font = new Font("宋体", 8.0f);
        //    titleText.TextColor = Color.Black;
        //    titleText.WordWrap = false;

        //    return titleText;
        //}

        //private static TextObject[] CreateBindingItem(string text, string bindingText, PointF startPos)
        //{
        //    var label = CreateLabel(text, startPos);

        //    var nextPoint = new PointF();
        //    nextPoint.X = (startPos.X + label.Width);
        //    nextPoint.Y = label.Top;

        //    var bindItem = CreateLabel(string.Format("[{0}]", bindingText), nextPoint);

        //    return new[] { label, bindItem };
        //}
        
        #endregion
    }

    /// <summary>
    ///     打印控件测试数据提供器
    /// </summary>
    internal class PrintFormatManageDataProvider:IPrint_DataProvider
    {

        public IPrint_ModulePrintDataProvider GetDataProviderByModuleID(string moduleID)
        {
            return new ModuleDataProvider(moduleID);
        }

        private class ModuleDataProvider:IPrint_ModulePrintDataProvider
        {
            private List<PrintFormatItemInfo> _cache=new List<PrintFormatItemInfo>();
            private string _moduleID = "";
            public ModuleDataProvider(string moduleID)
            {
                _moduleID = moduleID;
            }

            public IList<PrintFormatItemInfo> GetModulePrintFormatList()
            {
                return _cache;
            }

            public string GetModulePrintFormatByName(string printFormatName)
            {
                return _cache.Where(x => x.Name == printFormatName).Select(x => x.PrintFormat).FirstOrDefault();
            }

            public string GetDefaultFormat()
            {
                return _cache.FirstOrDefault().PrintFormat;
            }

            public void Add(string name, string formatData)
            {
                var temp = new PrintFormatItemInfo();

                temp.Name = name;
                temp.PrintFormat = formatData;
                temp.ModuleID = _moduleID;

                _cache.Add(temp);
            }

            public void Update(string name, string formatData)
            {
                _cache.FirstOrDefault(x => x.Name == name).PrintFormat = formatData;
            }

            public void Delete(string name)
            {
                _cache.Remove(x => x.Name == name);
            }

            public void SetDefault(string name)
            {
                
            }
        }
    }

    #region "测试实体"

    internal class MainOrder
    {
        public MainOrder()
        {
            Detail = new DetailWithDynamicColumnsCollection<DetailOrder>();
        }

        [PrintElement("往来单位", PrintElementLocation.PageHead)]
        public string UnitID { set; get; }

        [PrintElement("经手人", PrintElementLocation.PageHead)]
        public string Emplpoyee { set; get; }

        [PrintElement("AAA", PrintElementLocation.PageHead)]
        public string AAA { set; get; }

        [PrintElement("产品信息", PrintElementLocation.PageHead)]
        public ProductInfo Product { set; get; }

        [PrintElementDetailListAttribute("明细表")]
        public DetailWithDynamicColumnsCollection<DetailOrder> Detail { get; private set; }
    }

    internal class DetailOrder
    {
        public DetailOrder()
        {
            Product = new ProductInfo();
            Size = new DynamicColumn() { GroupName = "Size", DataType = typeof(decimal), HeaderText = "尺码" };

            //ColumnsGroup = new DynamicColumnsGroupCollection();
        }

        [PrintElement("单据MainID", PrintElementLocation.DetailInside)]
        public string MainID { set; get; }

        [PrintElement("产品信息", PrintElementLocation.DetailInside)]
        public ProductInfo Product { set; get; }

        [PrintElement("数量", PrintElementLocation.DetailInside)]
        public decimal Qty { set; get; }

        [PrintElement("单价", PrintElementLocation.DetailInside)]
        public decimal Price { set; get; }

        [PrintDynamicColumn("Size", typeof(decimal))]
        [PrintElement("尺码", PrintElementLocation.DetailInside)]
        public DynamicColumn Size { get; private set; }

        //[PrintElementAtrribute("", PrintElementLocation.DetailInside)]
        //public DynamicColumnsGroupCollection ColumnsGroup { get; private set; }
    }

    internal class VDetailOrder : DetailOrder
    {
        public VDetailOrder()
            : base()
        {
            Color = new DynamicColumn() { GroupName = "Color" };
        }

        [PrintDynamicColumnAttribute("Color", typeof(decimal))]
        [PrintElementAttribute("颜色", PrintElementLocation.DetailInside)]
        public DynamicColumn Color { get; private set; }
    }

    internal class ProductInfo
    {
        [PrintElementAttribute("MainID", PrintElementLocation.PageHead)]
        public string MainID { set; get; }

        [PrintElementAttribute("产品ID", PrintElementLocation.PageHead)]
        public string ProductID { set; get; }

    }

    #endregion
}
